package cn.zyjblogs.server.role.service.impl;

import cn.zyjblogs.server.role.dto.RolePageDto;
import cn.zyjblogs.server.role.mapper.RoleMapper;
import cn.zyjblogs.server.role.po.RoleDto;
import cn.zyjblogs.server.role.po.RolePo;
import cn.zyjblogs.server.role.service.RolePermissionRelService;
import cn.zyjblogs.server.role.service.RoleService;
import cn.zyjblogs.server.role.vo.RoleVo;
import cn.zyjblogs.starter.common.entity.context.BaseContext;
import cn.zyjblogs.starter.common.entity.response.HttpCode;
import cn.zyjblogs.starter.common.exception.CommonBusinessException;
import cn.zyjblogs.starter.common.utils.bean.BeanUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author zhuyijun
 */
@Service
@RequiredArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RolePo> implements RoleService {
    private final RoleMapper roleMapper;
    private final RolePermissionRelService rolePermissionRelService;
    private final IdentifierGenerator iGenerator;

    /**
     * 分页查询
     *
     * @param rolePageDto
     * @return com.baomidou.mybatisplus.core.metadata.IPage<cn.zyjblogs.rbac.server.user.vo.UserVo>
     * @author zhuyijun
     * @date 2022/9/19 下午6:52
     */
    @Override
    public IPage<RoleVo> findPage(RolePageDto rolePageDto) {
        IPage<RolePageDto> page = new Page<>(rolePageDto.getPage(), rolePageDto.getLimit());
        return roleMapper.findPage(page, rolePageDto, BaseContext.getTenantId());
    }

    /**
     * 通过用户id查询
     *
     * @param ids
     * @return java.util.List<cn.zyjblogs.rbac.server.user.vo.UserVo>
     * @author zhuyijun
     * @date 2022/9/19 下午6:52
     */
    @Override
    public List<RoleVo> findByIds(List<String> ids) {
        List<RolePo> rolePos = roleMapper.selectBatchIds(ids);
        if (CollectionUtils.isEmpty(rolePos)) {
            return Collections.emptyList();
        }
        return BeanUtils.map(rolePos, RoleVo.class);
    }

    /**
     * 查询当前租户下所有角色
     *
     * @param
     * @return java.util.List<cn.zyjblogs.server.role.vo.RoleVo>
     * @author zhuyijun
     * @date 2022/9/22 下午11:32
     */
    @Override
    public List<RoleVo> findList(String tenantId) {
        LambdaQueryWrapper<RolePo> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(tenantId != null, RolePo::getTenantId, tenantId);
        List<RolePo> rolePos = roleMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(rolePos)) {
            return new ArrayList<>();
        }
        return BeanUtils.map(rolePos, RoleVo.class);
    }

    /**
     * 保存角色
     *
     * @param roleDto
     * @return
     * @author zhuyijun
     * @date 2022/9/24
     */
    @Override
    public void save(RoleDto roleDto) {
        List<String> permissionIds = roleDto.getPermissionIds();
        if (CollectionUtils.isEmpty(permissionIds)) {
            throw new CommonBusinessException(HttpCode.BAD_REQUEST, "角色未关联权限资源");
        }
        roleDto.setPermissionIds(null);
        RolePo rolePo = BeanUtils.map(roleDto, RolePo.class);
        rolePo.setId(iGenerator.nextId(null).toString());
        rolePo.setCreateTime(LocalDateTime.now());
        rolePo.setCreateUserId(BaseContext.getUserId());
        rolePo.setTenantId(BaseContext.getTenantId());
        roleMapper.insert(rolePo);
        rolePermissionRelService.saveUpdate(rolePo.getId(), permissionIds, false);
    }

    /**
     * 更新角色
     *
     * @param roleDto
     * @return
     * @author zhuyijun
     * @date 2022/9/24
     */
    @Override
    public void update(RoleDto roleDto) {
        List<String> permissionIds = roleDto.getPermissionIds();
        if (CollectionUtils.isEmpty(permissionIds)) {
            throw new CommonBusinessException(HttpCode.BAD_REQUEST, "角色未关联权限资源");
        }
        roleDto.setPermissionIds(null);
        RolePo rolePo = BeanUtils.map(roleDto, RolePo.class);
        rolePo.setEditTime(LocalDateTime.now());
        rolePo.setEditUserId(BaseContext.getUserId());
        roleMapper.updateById(rolePo);
        rolePermissionRelService.saveUpdate(rolePo.getId(), permissionIds, true);
    }
}
